package com.uphone.kingmall.fragment;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import androidx.recyclerview.widget.GridLayoutManager;

import com.blankj.utilcode.util.LogUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.china.zhuoyue.R;
import com.lzy.okgo.model.HttpParams;
import com.lzy.okgo.model.Response;
import com.uphone.kingmall.activity.GoodsDetailActivity;
import com.uphone.kingmall.adapter.GoodsListAdapter;
import com.uphone.kingmall.base.BaseListFragment;
import com.uphone.kingmall.bean.GoodsListBean;
import com.uphone.kingmall.listener.onNormalRequestListener;
import com.uphone.kingmall.utils.CommonUtil;
import com.uphone.kingmall.utils.MyUrl;
import com.uphone.kingmall.utils.OkGoUtils;
import com.uphone.kingmall.utils.ToastUtils;

/**
 * author : SuMeng
 * e-mail : 986335838@qq.com
 * time   : 2019/05/22
 * desc   : xxxx 描述
 */
public class ShopCommendFragment extends BaseListFragment<GoodsListBean, GoodsListBean.DataBean> {
    private int id;

    public static ShopCommendFragment newInstance(int id) {
        Bundle args = new Bundle();
        ShopCommendFragment fragment = new ShopCommendFragment();
        fragment.id = id;
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    protected GoodsListBean getBeans() {
        return new GoodsListBean();
    }

    @Override
    protected HttpParams getParams() {
        HttpParams params = new HttpParams();
        params.put("shopId", id);
        params.put("limit", 100);
        return params;
    }

    @Override
    protected String getUrl() {
        return MyUrl.shopRemGoods;
    }

    @Override
    protected void itemClick(BaseQuickAdapter adapter, View view, int position) {
        LogUtils.e("点击跳转商品详情页" + position);
        CommonUtil.startIntent(getActivity(), GoodsDetailActivity.class, dataList.get(position).getGoodsId());
    }

    @Override
    protected BaseQuickAdapter initAdapter() {
        GridLayoutManager manager = new GridLayoutManager(getActivity(), 2);
        recyclerview.setLayoutManager(manager);
        GoodsListAdapter mAdapter = new GoodsListAdapter(R.layout.kingmall_item_goodslist_grid_add);
        mAdapter.setAddGoodsListener(new GoodsListAdapter.AddGoodsListener() {
            @Override
            public void onAddClick(GoodsListBean.DataBean bean) {
                addShoppingCart(bean);
            }
        });
        return mAdapter;
    }

    @Override
    public void initView(View view) {
    }

    @Override
    public void onResume() {
        super.onResume();
        loadData(1);
    }

    private void addShoppingCart(final GoodsListBean.DataBean dataBean) {
        HttpParams params = new HttpParams();
        params.put("goodsId", dataBean.getGoodsId());
        if (dataBean.getProps() != null) {
            String goodsPropIds = "";
            for (int i = 0; i < dataBean.getProps().size(); i++) {
                for (int j = 0; j < dataBean.getProps().get(i).getProp().size(); j++) {
                    goodsPropIds += dataBean.getProps().get(i).getProp().get(j).getGoodsPropId() + ",";
                }
            }
            if (!TextUtils.isEmpty(goodsPropIds)) {
                String str = goodsPropIds.substring(0, goodsPropIds.length() - 1);
                params.put("goodsPropIds", str);
            }
        }
        params.put("goodsNum", 1);
        OkGoUtils.normalRequest(MyUrl.addShopCar, params, new onNormalRequestListener() {
            @Override
            public void onSuccess(String response, int code) {
                if (code == 0) {
                    //点击添加按钮，刷新商品类别布局和底部购物车布局
                    if (mListener != null) mListener.onAddClick();
                    ToastUtils.showShortToast(getContext(), "成功加入购物车！");
                    loadData(1);
                }
            }

            @Override
            public void onError(Response<String> response) {

            }
        });
    }

    private AddGoodsListener mListener;

    public interface AddGoodsListener {
        void onAddClick();
    }

    public void setAddGoodsListener(AddGoodsListener mListener) {
        this.mListener = mListener;
    }
}
